package com.module.ad.toutiao;

import android.app.Activity;
import android.util.Log;
import android.view.View;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.module.privacy.module_ad.LogADUtils;
import com.module.privacy.module_ad.UIUtils;

import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * draw信息流
 */
public class TTDrawAd {

    private final static int count = 3;
    private static final String TAG = "头条广告";

    /**
     * 向list 插入广告、、加载是异步，需要改成同步。所以使用CountDownLatch。
     *
     * @param indexs 广告需要插入的位置。如[5,10,15] 分别插入5、10、15的位置
     */
    public static List loadDrawAd(Activity context, String posId, List list, int[] indexs) {
        LogADUtils.d(TAG, "启动头条流广告");

        //创建TTAdNative对象，createAdNative(Context context) context需要传入Activity对象
        float expressViewWidth = UIUtils.getScreenWidthDp(context);
        float expressViewHeight = UIUtils.getHeight(context);
        TTAdNative mTTAdNative = TTAdManagerHolder.get().createAdNative(context);
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(posId)
                .setExpressViewAcceptedSize(expressViewWidth, expressViewHeight) //期望模板广告view的size,单位dp
                .setAdCount(count) //请求广告数量为1到3条
                .build();

        //把广告异步线程改成同步, 3为头条最多广告条数
        CountDownLatch controller = new CountDownLatch(count);
        mTTAdNative.loadExpressDrawFeedAd(adSlot, new TTAdNative.NativeExpressAdListener() {
            @Override
            public void onError(int i, String s) {
                LogADUtils.d(TAG, "头条流广告加载失败:" + s);
                for (; ; ) {
                    controller.countDown();
                    if (controller.getCount() <= 0) {
                        break;
                    }
                }
            }

            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> listAd) {
                if (listAd == null) {
                    for (; ; ) {
                        controller.countDown();
                        if (controller.getCount() <= 0) {
                            break;
                        }
                    }
                }
                for (int i = 0; i < listAd.size(); i++) {
                    final int finalI = i;
                    TTNativeExpressAd ad = listAd.get(finalI);
                    ad.setCanInterruptVideoPlay(true);
                    ad.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
                        @Override
                        public void onAdClicked(View view, int type) {

                        }

                        @Override
                        public void onAdShow(View view, int type) {
                            LogADUtils.d(TAG, "头条流广告展示");
                        }

                        @Override
                        public void onRenderFail(View view, String msg, int code) {
                            controller.countDown();
                        }

                        @Override
                        public void onRenderSuccess(View view, float width, float height) {
                            int index = indexs[finalI];
                            if (list.size() > index) {
                                list.add(index, ad);
                            }
                            controller.countDown();
                        }
                    });
                    ad.render();
                }
            }
        });

        try {
            controller.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return list;
    }
}
